package development;

import iplayparticle.view.iplayparticleResources;

import java.util.Vector;

import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.component.BottomBar;
import state.component.Event;
import state.component.EventListener;
import state.component.GameMenu;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;
import development.bean.ChatRoom;
import development.group.FrmGroupMenu;
import development.socket.SocketClientUtil;

public class FrmListChatRoom extends GameForm implements EventListener {
  private final int DISTANCE = 21;
  private final int DX = 10;
  private final int DY = 48;
  
  public GameForm backForm;

  private GameImage tabImage;
  private GameImage tabFocusImage;
  private GameImage popupImage;

  private ImageText text8;
  private int currentRow;
  private int rowDy;

  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmListChatRoom();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    popupImage = new GameImage(iplayparticleResources.image_bg_popup, null, null);
    tabImage = new GameImage(iplayparticleResources.image_tab, null, null);
    tabFocusImage = new GameImage(iplayparticleResources.image_tab_focus, null, null);
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    currentRow = 0;
    rowDy = DY;

    // Create bottom bar
    GameMenu menu = new GameMenu(null, new String[] { "Update danh sách", "Về menu chính", "Thông tin TK" }, 3, this, Constants.MENU_STYLE, 0, GameConstants.SCREEN_HEIGHT - 45);
    BottomBar bottomBar = DrawUtils.getBottomBar(_manager);
    bottomBar.setLabels("Menu", "", "Trở về");
    bottomBar.setLeftMenu("Menu", menu);
    _manager.enableTouchPad();
  }

  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);

    // draw tab
    g.drawImage(tabFocusImage, 4, 4, GameConstants.TOP_LEFT_ANCHOR);
    g.drawImage(tabImage, 4, 4, GameConstants.TOP_LEFT_ANCHOR);
    text8.drawString2(g, "Chat room list", GameColor.WHITE_CODE, 40, 14, GameConstants.CENTER_ANCHOR);

    if (Global.chatRoomMap.size() == 0) {
      g.drawImage(popupImage, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
      text8.drawString2(g, "Chưa có phòng chat nào", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
    } else {
      // Set clip for draw room list
      if (GameConstants.IS_240x320_SCREEN) {
        g.setClip(10, 30, 221, 267);
      } else {
        g.setClip(8, 30, 305, 186);
      }

      // Draw room list
      Vector chatRoomEnum = Global.chatRoomMap.elements();
      for (int i = 0; i < chatRoomEnum.size(); i++) {
        g.setColor(GameColor.WHITE_CODE);
        g.drawLine(DX, rowDy - DISTANCE / 2 + i * DISTANCE, GameConstants.SCREEN_WIDTH - DX, rowDy - DISTANCE / 2 + i * DISTANCE);

        ChatRoom chatRoom = (ChatRoom) chatRoomEnum.elementAt(i);
        String roomName = "Phòng " + chatRoom.getName() + " (" + chatRoom.getConcurrentUser() + "/" + chatRoom.getMaxUser() + ")";
        int color = GameColor.WHITE_CODE;
        if (i == currentRow) {
          g.setColor(0x662221);
          g.fillRect(DX, rowDy - DISTANCE / 2 + i * DISTANCE + 2, GameConstants.SCREEN_WIDTH - 2 * DX, DISTANCE - 3);
          color = GameColor.YELLOW_CODE;
        }
        text8.drawString2(g, roomName, color, 26, rowDy + i * DISTANCE, GameConstants.VCENTER_LEFT_ANCHOR);
      }
    }
  }

  public void onActionPerform(Event event) {
    String action = event.getAction();
    if (action.equals("Update danh sách")) { // Cập nhật danh sách
      SocketClientUtil.requestChatRoomList();
    } else if (action.equals("Về menu chính")) { // Về Menu chính
      GameGlobal.nextState(FrmGroupMenu.instance(), null);
    } else if (action.equals("Thông tin TK")) { // Thông tin TK
    }
  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.UP:
    case Key.K_2:
      if (Global.chatRoomMap.size() == 0) {
        return;
      }
      if (currentRow > 0) {
        currentRow--;
      } else {
        currentRow = Global.chatRoomMap.size() - 1;
      }
      break;
    case Key.DOWN:
    case Key.K_8:
      if (Global.chatRoomMap.size() == 0) {
        return;
      }
      if (currentRow < Global.chatRoomMap.size() - 1) {
        currentRow++;
      } else {
        currentRow = 0;
      }
      break;
    case Key.FIRE:
    case Key.K_5:
      if (Global.chatRoomMap.size() == 0) {
        return;
      }
      break;
    case Key.SOFT_RIGHT:
      if (backForm != null) {
        GameGlobal.nextState(backForm, null);
        backForm = null;
      } else {
        GameGlobal.nextState(FrmGroupMenu.instance(), null);
      }
      break;
    }
  }

  public void destroy() {
    popupImage = null;
    text8 = null;
    tabImage = null;
    tabFocusImage = null;
  }
}
